package com.yangli.leecode.mashib.interview;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;

/**
 * @Description
 * @Author liyang
 * @Date 2023/4/28 10:05
 */
public class SeventySix {
    public static void main(String[] args){
    }


    public static int kEmptySlot(int[] bulbs, int k){

        int n = bulbs.length;
        int[] days = new int[n];//将第i小时的value号灯亮数组bulbs转变为第i号灯在第value小时亮，由于要计算亮灯泡间隔k个数不亮，要以灯泡序为基地
        for (int i = 0; i < n; i++) {
            days[bulbs[i] - 1] = i + 1;
        }
        //l-r窗口，窗口中间的值都大于l-r的值表面，l-r灯先亮，统计答案
        int ans = Integer.MAX_VALUE;
        for (int left = 0, right = k + 1, mid = 1; right < n; mid++) {
            //1,中间值小于两边，left调到mid位置
            //2,中间值到达right，计算答案。并且中间值都大于right，则left到达right。
            if (days[mid] <= Math.max(days[left], days[right])) {
                if (mid == right) {
                    ans = Math.min(ans, Math.max(days[left], days[right]));
                }
                left = mid;
                right = mid + k + 1;

            }
        }
        return ans == Integer.MAX_VALUE ? -1 : ans;
    }


    public static int[] avoidFlood(int[] rains){
        boolean[] flag = new boolean[Integer.MAX_VALUE];


        int[] ans = new int[rains.length];
        HashMap<Integer, LinkedList<Integer>> hashMap = new HashMap<>();
        for (int i = 0; i < rains.length; i++) {
            if (rains[i] != 0) {
                if (!hashMap.containsKey(rains[i])) {
                    hashMap.put(rains[i], new LinkedList<Integer>());
                }
                hashMap.get(rains[i]).addLast(i);
            }

        }
        //没抽干的湖泊，下雨了加到set里面，抽干从set移除
        HashSet<Integer> set = new HashSet<>();
        //当前湖泊，下一个湖泊位置
        PriorityQueue<Work> heap = new PriorityQueue<>();

        for (int i = 0; i < rains.length; i++) {
            if (rains[i] != 0) {
                if (set.contains(rains[i])) {//包含重复湖泊
                    return new int[0];
                }
                set.add(rains[i]);
                hashMap.get(rains[i]).pollFirst();
                if (!hashMap.get(rains[i]).isEmpty()) {
                    heap.add(new Work(rains[i], hashMap.get(rains[i]).pollFirst()));
                }
                ans[i] = -1;

            } else {
                if (heap.isEmpty()) {
                    ans[i] = 1;
                } else {
                    Work cur = heap.poll();
                    set.remove(cur.lake);
                    ans[i] = cur.lake;
                }
            }

        }
        return ans;
    }

    public static class Work implements Comparable<Work> {
        int lake;
        int nextRain;

        public Work(int l, int r){
            lake = l;
            nextRain = r;
        }

        @Override
        public int compareTo(@NotNull SeventySix.Work o){
            return nextRain - o.nextRain;
        }
    }

}
